package com.ebupt.migu.music.userGroup.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.userGroup.entity.SplitGroupDTO;
import com.ebupt.migu.music.userGroup.service.LabelService;

import freemarker.template.TemplateException;

/**
 * Created by Intellij IDEA.
 * User: liuyangyang@cd.ebupt.com
 * Date: 2019/9/4
 */

@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LabelService       labelService;
    @Resource
    private ReqestParamUtil    reqestParamUtil;

    /**
     * 查询省市划分列表
     *
     * @return
     */
    @RequestMapping("/area")
    public ResultObject queryAreaList() {
        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);
        return labelService.queryAreaList(paramMap);
    }

    /**
     * 查询年龄段划分列表
     *
     * @return
     */
    @RequestMapping("/age")
    public ResultObject queryAgeDivisionList() {
        return labelService.queryAgeDivisionList();
    }

    /**
     * 查询等级划分列表
     *
     * @return
     */
    @RequestMapping("/grade")
    public ResultObject queryGradeDivisionList() {
        return labelService.queryGradeDivisionList();
    }

    /**
     * 通过标签创建人群
     *
     * @return
     */
    @RequestMapping("/create")
    public ResultObject createByLabel() throws IOException, TemplateException {
        Map<String, Object> paramMap = reqestParamUtil.getParams2(request);
        return labelService.createByLabel(paramMap);
    }

    /**
     * 通过人群名称查询人群ID
     *
     * @return
     */
    @RequestMapping("/queryByName")
    public ResultObject queryByName() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return labelService.queryByName(params);
    }

    @RequestMapping("/refresh")
    public void refresh() {
        labelService.refresh();
    }

    /**
     * 获取白金会员类型
     *
     * @return
     */
    @RequestMapping("/vipCode")
    public ResultObject vipCode() {
        return labelService.vipCode();
    }

    /**
     * 创建临时客群
     *
     * @return
     */
    @RequestMapping("/create/tmp")
    public ResultObject createTmp() {
        Map<String, Object> paramMap = reqestParamUtil.getParams2(request);
        return labelService.createTmp(paramMap);
    }

    /**
     * 创建临时客群
     *
     * @return
     */
    @RequestMapping("/static/customer/info")
    public ResultObject staticCustomerInfo() {
        Map<String, Object> paramMap = reqestParamUtil.getParams2(request);
        return labelService.queryStaticCustomerInfo(paramMap);
    }

    /**
     * <p>
     * 【客群分箱】
     * </p>
     * 获取标签平台sessionId
     *
     * @return sessionId
     */
    @PostMapping("/getSessionId")
    public ResultObject<Map<String, String>> getSessionId() {
        // 获取登录用户信息
        SysUser sysUser = AuthBaseUtils.getSessionUser(request);
        return labelService.getSessionId(sysUser);
    }

    /**
     * <p>
     * 【客群分箱】
     * </p>
     * 分箱完成后批量增加新客群信息
     *
     * @param splitGroupDTO
     *            分箱数据传输类
     * @return 是否分箱成功
     */
    @PostMapping("/batchSaveCustomer")
    public ResultObject<String> batchSaveCustomerInfo(@RequestBody SplitGroupDTO splitGroupDTO) {
        // 获取登录用户信息
        SysUser user = AuthBaseUtils.getSessionUser(request);
        return labelService.batchSaveCustomerInfo(splitGroupDTO, user);
    }

    /**
     * 视频彩铃标签-级联菜单查询
     *
     * @return
     */
    @PostMapping("/video/dropdown")
    public ResultObject dropDownMenus() {
        // 获取登录用户信息
        // SELECT distinct extracted_content1, id_1 FROM dmid_source_material_new_map ORDER BY extracted_content1, id_1 LIMIT 0, 10;
        //
        // SELECT distinct extracted_content2, id_2 FROM dmid_source_material_new_map WHERE id_1 = 3 ORDER BY extracted_content2, id_2 LIMIT 0, 10;
        //
        // SELECT distinct extracted_content3, id_3 FROM dmid_source_material_new_map WHERE id_1 = 1 AND id_2 = 1 ORDER BY extracted_content3, id_3
        // LIMIT 0, 10;
        Map<String, Object> params = reqestParamUtil.getParams2(request);
        return labelService.dropDownMenus(params);
    }
}
